import argparse
from PIL import Image,ImageFont
import lib
import os,shutil
import rich
argparser = argparse.ArgumentParser()
argparser.add_argument('filepath', type=str,
                       help='The path of the file to be processed')
argparser.add_argument('-f', '--font', type=str, default="arial.ttf",
                       help='The font in the generated picture')
argparser.add_argument(
    '-fs', '--fontsize', type=int, default=20, help='The font size in the generated picture')
argparser.add_argument('-fc', '--fontcolor', type=str, default="black",
                       help='Number of colors')
argparser.add_argument('-he', '--height', type=int, default=-1,
                       help='Number of target rows, default is auto')
argparser.add_argument('-w', '--width', type=int, default=-1,
                       help='Number of target columns, default is auto')
argparser.add_argument('-n', '--num', type=int, default=10,
                       help='Number of colors')
argparser.add_argument('-b', '--background', type=str, default="white",
                       help='Number of colors')
args = argparser.parse_args().__dict__
image=Image.open(args['filepath'])
if args["width"]<=0 and args["height"]<=0:
    args["width"]=80
if args["height"]<=0:
    args["height"]=int(args["width"]/image.size[0]*image.size[1])
if args["width"]<=0:
    args["width"]=int(args["height"]/image.size[1]*image.size[0])
print(args)
colormap=lib.mainColor.ColorMap(
    image,
    (args["width"],args["height"]),
    args["num"],
    ImageFont.truetype(args["font"], args["fontsize"]),
    args["background"],
    args["fontcolor"]
)
if os.path.exists("diamondPicture"):
    while True:
        rich.print("[yellow]Diamond picture already exists, do you want to overwrite it?[/yellow](Y/n)",end="")
        answer=input()
        if answer.lower()=="n":
            os._exit(1)
        elif answer.lower()=="y":
            break
    shutil.rmtree("diamondPicture")
os.makedirs("diamondPicture")
colormap.pModeImage.save("diamondPicture/target.png")
colormap.printImage.save("diamondPicture/primeval.png")
colormap.printColorMapImage.save("diamondPicture/colors.png")